﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using RRRSRoguelike.Entities;
using RRRSRoguelike;
using RRRSRoguelike.Managers;
using System.Timers;
using System.Threading;
using System.IO;
using System.Reflection;


namespace RRRSRoguelike
{
    /// <summary>
    /// Main entry point.
    /// </summary>
    class RRRSRogueLike
    {
        /// <summary>
        /// Main Entry Point
        /// </summary>
        /// <param name="args"></param>
        private static void Main(string[] args)
        {

            PrepareContentFolder();

            //Set width height and title of console.
            SetUpConsole();
            


            //Run the fun.
            GameManager.Instance.Run();
        }

        private static void PrepareContentFolder()
        {
            //Prepare Custom Content Folder
            string sourcePath = AppDomain.CurrentDomain.BaseDirectory;

            sourcePath = Path.Combine(sourcePath, "CustomContent");

            string targetPath;

            if (Properties.Settings.Default.CustomFolder == "-1")
            {
                targetPath = Path.Combine(Environment.GetFolderPath(
                    Environment.SpecialFolder.MyDocuments), @"RRRSRoguelike\CustomContent");
                Properties.Settings.Default.CustomFolder = targetPath;
            }
            else
            {
                targetPath = Properties.Settings.Default.CustomFolder;
            }


            CopyFolder(sourcePath, targetPath, false);
        }

        private static void CopyFolder(string sourcePath, string targetPath, bool overwrite)
        {
            string fileName;
            string destFile;
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist. 
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);

                    //Create targetPath
                    Directory.CreateDirectory(targetPath);
                    
                    if(overwrite || !System.IO.File.Exists(destFile))
                    {
                        System.IO.File.Copy(s, destFile, overwrite);
                    }

                }
                string[] folders = Directory.GetDirectories(sourcePath);
                foreach (string s in folders)
                {
                    string name = Path.GetFileName(s);
                    string dest = Path.Combine(targetPath, name);
                    CopyFolder(s, dest,false);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }
        }

        /// <summary>
        /// Sets the width, height and title of console. Based on the resources file.
        /// </summary>
        private static void SetUpConsole()
        {
            //Added console height and width to constants.  Resizes on draw if user changes.

            GameManager.SetConsoleSize(Constants.ConsoleWidth, Constants.ConsoleHeight);
                   

            //Prints the name and version number at the top of the window.
            Console.Title = "RRRSRogueLike" + " v" + Assembly.GetEntryAssembly().GetName().Version;
        }

    }
}

